package com.myplans.server.service;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;


public class FileUploadServlet extends HttpServlet {

	private static final long serialVersionUID = 8994469582398118999L;
	private static Logger logger = Logger.getLogger(FileUploadServlet.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		logger.info("Shouldn't be here!");
//		FileInputStream fis;
		try {
//			fis = new FileInputStream("DPgXL.jpg");
//			BufferedInputStream bis = new BufferedInputStream(fis);
//			byte[] bytes = new byte[bis.available()];
//			response.setContentType("image/jpeg");
			OutputStream os = response.getOutputStream();
//			bis.read(bytes);
			String image = request.getParameter("image");
			byte[] bytes = Base64.decodeBase64(image.getBytes());
 			os.write(bytes);
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {

		logger.info("Processing upload request.");
		
		FileItemFactory fileFactory = new DiskFileItemFactory();
		ServletFileUpload uploadServlet = new ServletFileUpload(fileFactory);
		try {
			List items = uploadServlet.parseRequest(request);
			Iterator iterator = items.iterator();
			FileItem item = (FileItem) iterator.next();
			byte[] itemData = item.get();
			InputStream in = new ByteArrayInputStream(itemData);
			BufferedImage bImageFromConvert = ImageIO.read(in);
			ImageIO.write(bImageFromConvert, "jpg", new File(
					String.valueOf(System.currentTimeMillis())+".jpg"));
			response.setContentType("text/html");
			
			String s = "data:image/jpg"+";base64," + new String(Base64.encodeBase64(itemData));
		    response.getOutputStream().write(s.getBytes());
		    response.getOutputStream().flush();
		} catch (Exception e) {
			try {
				response.getOutputStream().write("Picture upload failed".getBytes());
				response.getOutputStream().flush();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			logger.error("Exception while processing the upload request: " + e.getMessage());
			e.printStackTrace();
		}
		
	}
}
